package com.example.mqtt.service.Imp;

import com.example.mqtt.entity.AngleData;
import com.example.mqtt.entity.PersonAngles;
import com.example.mqtt.service.FakeDataService;
import com.example.mqtt.tools.SlurryTools;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service
public class FakeDataServiceImp implements FakeDataService {
   private final Random random = new Random();
    @Override
    public  AngleData generateRandomAngleData(String rowingNo,int index,int group) {
        long timestamp = System.currentTimeMillis();
        List<PersonAngles> angles = generateRandomAngles(index,timestamp);
        AngleData angleData = new AngleData();
        angleData.setGroup(group);
        angleData.setId(SlurryTools.getUUID());
        angleData.setRowingNo(rowingNo);
        angleData.setTimeStamp(timestamp);
        angleData.setAngles(angles);
        return angleData;
    }



    private List<PersonAngles>  generateRandomAngles(int index,long timeStamp) {
        List<PersonAngles> angles = new ArrayList<>();
        for (int i = index; i < index+2; i++) {

            PersonAngles personAngles = new PersonAngles(
                    roundToTwoDecimalPlaces(random.nextDouble() * 180), // leftElbow
                    roundToTwoDecimalPlaces(random.nextDouble() * 180), // rightElbow
                    roundToTwoDecimalPlaces(random.nextDouble() * 180), // leftHip
                    roundToTwoDecimalPlaces(random.nextDouble() * 180), // rightHip
                    roundToTwoDecimalPlaces(random.nextDouble() * 180), // leftKnee
                    roundToTwoDecimalPlaces(random.nextDouble() * 180), // rightKnee
                    roundToTwoDecimalPlaces(random.nextDouble() * 180), // leftLegToY
                    roundToTwoDecimalPlaces(random.nextDouble() * 180), // rightLegToY
                    roundToTwoDecimalPlaces(random.nextDouble() * 180), // leftBodyToY
                    roundToTwoDecimalPlaces(random.nextDouble() * 180), // rightBodyToY
                    roundToTwoDecimalPlaces(random.nextDouble() * 180), // leftArmBodyToY
                    roundToTwoDecimalPlaces(random.nextDouble() * 180), // rightArmBodyToY
                    String.valueOf(i), // personPos
                    timeStamp, // timestamp
                    roundToTwoDecimalPlaces(random.nextDouble() * 180), // ankleX
                    roundToTwoDecimalPlaces(random.nextDouble() * 180), // ankleY
                    roundToTwoDecimalPlaces(random.nextDouble() * 180), // kneeX
                    roundToTwoDecimalPlaces(random.nextDouble() * 180), // kneeY
                    roundToTwoDecimalPlaces(random.nextDouble() * 180), // hipX
                    roundToTwoDecimalPlaces(random.nextDouble() * 180), // hipY
                    roundToTwoDecimalPlaces(random.nextDouble() * 180), // shoulderX
                    roundToTwoDecimalPlaces(random.nextDouble() * 180), // shoulderY
                    roundToTwoDecimalPlaces(random.nextDouble() * 180), // elbowX
                    roundToTwoDecimalPlaces(random.nextDouble() * 180), // elbowY
                    roundToTwoDecimalPlaces(random.nextDouble() * 180), // wristX
                    roundToTwoDecimalPlaces(random.nextDouble() * 180)  // wristY
            );
            angles.add(personAngles);
        }
        return angles;
    }

    private static double roundToTwoDecimalPlaces(double value) {
        return Math.round(value * 100.0) / 100.0;
    }

}
